package com.ktjy.ktmall.servlet;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.ktjy.ktmall.entity.AjaxResult;
import com.ktjy.ktmall.entity.Page;
import com.ktjy.ktmall.entity.Role;
import com.ktjy.ktmall.entity.User;
import com.ktjy.ktmall.service.RoleService;
import com.ktjy.ktmall.service.UserService;
import com.ktjy.ktmall.service.impl.RoleServiceImpl;
import com.ktjy.ktmall.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "RoleServlet", urlPatterns = "/uni/manager/role")
public class RoleServlet extends HttpServlet {
    private RoleService service = null;

    @Override
    public void init() throws ServletException {
        service = new RoleServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取从小程序端请求过来的参数
        String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        System.out.println(params);
        // 3、将请求参数字符串转换成JSONObject对象，之后从JSONObject对象中取出值
        JSONObject entries = JSONUtil.parseObj(params);
        String opr = entries.get("opr", String.class);
        AjaxResult result = null;
        //通过opr区分当前操作是查询、删除、增加等等
        //opr=list
        switch (opr) {
            case "page": {
                //分页查询
                int currPageNo = 1;  // 当前页码

                if (request.getParameter("currPageNo") != null) {
                    currPageNo = Integer.valueOf(request.getParameter("currPageNo"));
                }
                int pageSize = 5;  // 每页显示的数据行数

                if (request.getParameter("pageSize") != null) {
                    pageSize = Integer.parseInt(request.getParameter("pageSize"));
                }
                // 4、调用serivce中查询总记录数的方法
                int count = service.count();
                // 5、调用service中查询商品的分页列表数据
                List<Role> list = service.getRoleListPage(currPageNo, pageSize);
                // 6、封装Page对象
                Page page = new Page();
                page.setCurrPageNo(currPageNo);
                page.setPageSize(pageSize);
                page.setTotalCount(count);
                page.setList(list); //
                result = AjaxResult.success("角色列表加载成功!", page);
                break;
            }
            case "insert": {
                Role role = entries.get("role", Role.class);
                int count = service.insertRole(role);
                if (count > 0) {
                    result = AjaxResult.success("新增成功!");
                } else {
                    result = AjaxResult.success("新增失败!");
                }
                break;
            }
            case "selectRoleById":
                //Integer id = Integer.valueOf(entries.get("id", Integer.class));
                String roleId = request.getParameter("roleId");
                Role role = service.selectRoleById(Integer.parseInt(roleId));
                result = AjaxResult.success("角色查询成功", role);
                break;
            case "update":
                role = entries.get("role", Role.class);
                int i = service.updateRole(role);
                if (i > 0) {
                    result = AjaxResult.success("修改成功!");
                } else {
                    result = AjaxResult.error("修改失败!");
                }
                break;
            case "delete":
                roleId = request.getParameter("roleId");
                i = service.deleteRole(Integer.parseInt(roleId));
                if (i > 0) {
                    result = AjaxResult.success("删除成功!");
                } else {
                    result = AjaxResult.error("删除成功!");
                }
                break;
        }
        // 将result对象转换成JSON字符串再响应到小程序端
        out.print(JSONArray.toJSONString(result));
        out.flush();
        out.close();
    }
}
